package com.kafka.dongli.config;

import com.kafka.dongli.config.interceptor.CustomerConsumerInterceptor;
import com.kafka.dongli.config.interceptor.CustomerProducerInterceptor;
import com.kafka.dongli.config.partitioner.CustomerPartitioner;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.RoundRobinAssignor;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: KafkaConfig
 * @Description:
 * @Author: 橙哈哈
 * @Date: 2025-09-19 23:47
 **/
@Configuration
public class KafkaConfig {



    public static final String TOPIC = "simple-topic";




    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${spring.kafka.producer.key-serializer}")
    private String keySerializer;

    @Value("${spring.kafka.producer.value-serializer}")
    private String valueSerializer;

    /**
     * 自定义分区策略-生产者相关配置
     * @return
     */
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
        props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, CustomerPartitioner.class.getName());
        //添加一个拦截器
        props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, CustomerProducerInterceptor.class.getName());
        return props;
    }
    /**
     * 自定义分区策略-生产者创建工厂
     * @return
     */
    public ProducerFactory<String, ?> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }
    /**
     * 自定义分区策略- kafkaTemplate 覆盖默认配置类中的kafkaTemplate
     * @return
     */
    @Bean
    @Qualifier("customerPartitioner")
    public KafkaTemplate<String, ?> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }



    /**
     * 通过bean方式创建topic ，名称为 simple-topic，分区数为 3，副本因子为 1 副本因子和部署的节点数有关 部位为0 也不能超过节点数
     * @return
     */
//    @Bean
//    public NewTopic createTopic() {
//        return new NewTopic(TOPIC, 3, (short) 1);
//    }


    //对topic进行更新 分区只能增加 无法减少
//    @Bean
//    public NewTopic updateNewTopic() {
//        return new NewTopic(TOPIC, 9,  (short)1);
//    }

    /**
     * 分区主题
     * @return
     */
    @Bean
    public NewTopic partitionTopic() {
        return new NewTopic("partition-topic", 3,  (short)1);
    }

    @Bean
    public NewTopic domainTopic() {
        return new NewTopic("domain-topic", 3,  (short)1);
    }


    @Bean
    public NewTopic callBackTopic() {
        return new NewTopic("callBack-topic", 3,  (short)1);
    }





    /**
     * 消费者相关配置
     *
     * @return
     */
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keySerializer);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueSerializer);
        //props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        //指定使用轮询的消息消费分区器
        props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, RoundRobinAssignor.class.getName());
        //添加一个消费者截器
        props.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, CustomerConsumerInterceptor.class.getName());
        return props;
    }

    /**
     * 消费者创建工厂
     *
     * @return
     */
    @Bean
    public ConsumerFactory<String, String> ourConsumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    /**
     * 创建监听器容器工厂
     * @param ourConsumerFactory
     * @return
     */
    @Bean
    public KafkaListenerContainerFactory<?> ourKafkaListenerContainerFactory(ConsumerFactory<String, String> ourConsumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, String> listenerContainerFactory = new ConcurrentKafkaListenerContainerFactory<>();
        listenerContainerFactory.setConsumerFactory(ourConsumerFactory);
        return listenerContainerFactory;
    }








}
